﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SB.Logging;
using System.IO;
using System.Xml;
using System.Collections;

using SB.Tools;

namespace SB.UserSettings
{
    class UserSettingsXML : IUserSettingsXML
    {
        private static readonly ILogger log = LoggingFacade.GetLogger(typeof(UserSettingsXML));
        
        string xmlFileName = "userSettings.xml";
        string programName = "ProgramName";

        private XmlDocument doc = null;

        public void Initialize(string programName)
        {
            this.programName = programName;

            OpenXmlFile(true);
        }

        public void Initialize(string programName, string xmlFileName)
        {
            this.programName = programName;
            this.xmlFileName = xmlFileName;

            OpenXmlFile(true);
        }

        private void OpenXmlFile(bool create)
        {
            FileInfo file = new FileInfo(xmlFileName);

            if (!file.Exists && create)
            {
                XmlTextWriter writer = new XmlTextWriter(xmlFileName, null);
                writer.WriteStartDocument();
                writer.WriteStartElement(programName);
                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Close();
            }

            doc = new XmlDocument();
            doc.Load(xmlFileName);
        }

        public SortableBindingList<UserSetting> ReadUserSettings(params string[] path)
        {
            XmlNode node = doc[programName];
            SortableBindingList<UserSetting> settings = new SortableBindingList<UserSetting>();

            try
            {
                foreach (string p in path)
                {
                    node = node[p];
                }

                foreach (XmlNode n in node.ChildNodes)
                {
                    settings.Add(new UserSetting(n.Name, n.InnerText));
                }
            }
            catch { }

            return settings; 
        }

        public void WriteUserSettings(SortableBindingList<UserSetting> settings, UserSettingsFacade.WriteTypes writeType, bool create, params string[] path)
        {
            XmlNode node = doc[programName];
            XmlNode oldNode = null;
            
            foreach (string p in path)
            {
                try
                {
                    if (node != null)
                        oldNode = node;
                    node = node[p];
                }
                catch
                {

                }

                if (node == null)
                {
                    if (create)
                    {
                        XmlNode tmp = doc.CreateNode(XmlNodeType.Element, p, null);
                        oldNode.AppendChild(tmp);
                        node = tmp;
                    }
                    else
                    {
                        throw new Exception("Unknown XML-Tag '" + p + "'");
                    }
                }
            }

            if (writeType == UserSettingsFacade.WriteTypes.Overwrite)
                node.RemoveAll();

            foreach (UserSetting s in settings)
            {
                XmlNode newNode = doc.CreateNode(XmlNodeType.Element, s.Name, null);

                if (s.Text != null)
                    newNode.InnerText = s.Text;

                node.AppendChild(newNode);
            }

            doc.Save(xmlFileName);
        }
    }
}
